package cn.ljy.uem.service.impl;

import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.utils.page.Pagination;
import cn.ljy.uem.dao.SchoolStudentMapper;
import cn.ljy.uem.dao.SchoolTeachingTaskMapper;
import cn.ljy.uem.dao.SchoolTeachingTaskStudentMapper;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolTeachingTaskStudentQuery;
import cn.ljy.uem.service.SchoolTeachingTaskStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.*;


/**
 * @description 教学班学生模块的业务层接口
 * @author 廖振豪
 * @date 2022-7-18 17:22
 */
@Service
@Slf4j
public class ISchoolTeachingTaskStudentService implements SchoolTeachingTaskStudentService {

    @Autowired
    private SchoolTeachingTaskStudentMapper mapper;

    @Autowired
    private SchoolStudentMapper schoolStudentMapper;

    @Autowired
    private SchoolTeachingTaskMapper schoolTeachingTaskMapper;



    @Override
    public SchoolTeachingTaskStudent get(Serializable teachingTaskId, Serializable studentId) throws MyWebException {
        SchoolTeachingTaskStudent result=null;
        //适合读取数据前的验证
        this.beforeGet(teachingTaskId, studentId);
        //从数据库读取记录
        result=mapper.findByTeachingTaskIdAndStudentId(teachingTaskId, studentId);
        //读取后的数据处理，例如字段修改，添加关联属性
        if(result!=null){
            this.afterGet(result);
        }
        return result;
    }

//    @Override
    public void save(@NotNull(message="传递参数不能为null") @Valid SchoolTeachingTaskStudent student) {
        //适合保存数据前的验证
        beforeSave(student);
        //保存数据后的处理
            //将前端传递过来的集合转换为set集合,顺便去重
            Set<Long> studentIdsSet = new HashSet<Long>(Arrays.asList(student.getStudentIds()));
            //获取原来的学生
            List<Long> studentsOrigin=mapper.findAllStudentByTeachingTaskId(student.getTeachingTaskId());
            //处理教学任务和学生的关系
            for(Long studentId:studentIdsSet){
                if(studentsOrigin.contains(studentId)){
                    throw new MyWebException(schoolStudentMapper.findById(studentId).getName() + "已经存在");
                }
            }
            for (Long studentId:studentIdsSet){
                if (student.getType() != null){
                    mapper.save(student.getTeachingTaskId(),studentId,student.getType());
                }else{
                    student.setType(2);
                    mapper.save(student.getTeachingTaskId(),studentId,student.getType());
                }

            }
        afterSave(student);
        }

    @Override
    public void update(@NotNull(message="传递参数不能为null") @Valid SchoolTeachingTaskStudent student) {
        //适合更新数据前的验证
        beforeUpdate(student);
        mapper.update(student);
        //更新数据后的处理
        afterUpdate(student);
    }


    @Override
    public void delete(@NotNull(message="传递参数不能为null") Integer teachingTaskId,@NotEmpty(message = "操作失败：请选择要移除的记录") Serializable[] studentIds) {
        beforeDelete(teachingTaskId,studentIds);
        for (Serializable id : studentIds) {
            mapper.delete(teachingTaskId,id);
        }
        afterDelete(teachingTaskId,studentIds);
    }

    @Override
    public List<SchoolTeachingTaskStudent> findAll(SchoolTeachingTaskStudentQuery query) {
        List<SchoolTeachingTaskStudent> list=new ArrayList<>();
        beforeFindAll(query);
        list=mapper.findAll(query);
        afterFindAll(list);
        return list;
    }

    @Override
    public Pagination findAll(Integer page, Integer pageSize, SchoolTeachingTaskStudentQuery query) {
        beforeFindAll(query);
        //验证并计算相关分页数据
        Pagination pagination = new Pagination(page, pageSize,mapper.count(query));
        //为当前页的数据集赋值
        pagination.setData(mapper.find(pagination.getOffset(),pagination.getPageSize(),query));
        afterFindAll(pagination.getData());
        return pagination;
    }





    protected void beforeGet(Serializable teachingTaskId, Serializable studentId) {
    }
    protected void afterGet(SchoolTeachingTaskStudent student) {
    }

    protected void beforeUpdate(SchoolTeachingTaskStudent schoolTeachingTaskStudent) {

    }

    protected void afterUpdate(SchoolTeachingTaskStudent schoolTeachingTaskStudent){}

    //在添加函数之前执行
    protected void beforeSave(SchoolTeachingTaskStudent schoolTeachingTaskStudent){};

    //在添加函数之后执行
    protected void afterSave(SchoolTeachingTaskStudent schoolTeachingTaskStudent){}

    protected  void beforeDelete(Serializable teachingTaskId,Serializable[] ids){

    };

    protected void afterDelete(Serializable teachingTaskId,Serializable[] ids){}



    protected void beforeFindAll(SchoolTeachingTaskStudentQuery query) {
    }
    //教学班学生多表查询
    protected void afterFindAll(List<SchoolTeachingTaskStudent> list) {
        for (SchoolTeachingTaskStudent schoolTeachingClassStudent:list){
            if(schoolTeachingClassStudent.getStudentId()!=null){
                SchoolStudent schoolStudent= schoolStudentMapper.findById(schoolTeachingClassStudent.getStudentId());
                schoolTeachingClassStudent.setSchoolStudent(schoolStudent);
            }
            if(schoolTeachingClassStudent.getTeachingTaskId()!=null){
                SchoolTeachingTask studentTask = schoolTeachingTaskMapper.findById(schoolTeachingClassStudent.getTeachingTaskId());
                schoolTeachingClassStudent.setSchoolTeachingTask(studentTask);
            }
        }
    }

}
